package com.example.yls.demoa;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;

/**
 * Created by yls on 2018/11/13.
 */

public class StudentAdapter extends BaseAdapter{
    public StudentAdapter(Context context, ArrayList<Student> studentArrayList) {
        this.context = context;
        this.studentArrayList = studentArrayList;
    }

    private Context context;
    private ArrayList<Student> studentArrayList;
    @Override
    public int getCount() {
        return studentArrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return studentArrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if(view==null){
            view= LayoutInflater.from(context).inflate(R.layout.list_item,viewGroup,false);
            StudentViewHolder holder = new StudentViewHolder();
            holder.txtNo = view.findViewById(R.id.txt_no);
            holder.txtName = view.findViewById(R.id.txt_name);
            holder.txtAge = view.findViewById(R.id.txt_age);

            view.setTag(holder);
        }


        StudentViewHolder holder = (StudentViewHolder) view.getTag();
        Student s = studentArrayList.get(i);

        holder.txtNo.setText(s.getStunumber());
        holder.txtName.setText(s.getName());
        holder.txtAge.setText(String.valueOf(s.getAge()));



        return view;
    }
    public void changeData(ArrayList<Student> studentArrayList) {
        this.studentArrayList = studentArrayList;
        notifyDataSetChanged();
    }
}
